#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCharts>
#include <QBarSeries>
#include <QPieSeries>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QChart *chart = new QChart();
    chart->setTitle("曲线图示例");

    QLineSeries *series = new QLineSeries();//创建连线
    series->append(QPointF(0, 6));//往连线中加入点
    series->append(QPointF(2, 4));
    series->append(QPointF(3, 8));
    series->append(QPointF(4, 4));
    series->append(QPointF(5, 9));
    series->append(QPointF(6, 3));
    series->append(QPointF(7, 6));
    series->setName("曲线A");
    chart->addSeries(series);//把连线加入到图标

    chart->createDefaultAxes();//创建坐标轴
    chart->axisX()->setTitleText("X轴");//设置XY轴的标题
    chart->axisY()->setTitleText("Y轴");
    chart->setAnimationOptions(QChart::AllAnimations);
//    QChartView *chartView = new QChartView(chart);
//    chartView->setRenderHint(QPainter::Antialiasing);//设置线条抗锯齿效果

    this->ui->widget->setChart(chart);//把图表加进图表视图窗口



    // 创建一个柱状图
    chart = new QChart();
    chart->setTitle("Simple Bar Chart");

    // 创建一些数据点
    QBarSet *set0 = new QBarSet("Jane");
    QBarSet *set1 = new QBarSet("John");
    QBarSet *set2 = new QBarSet("Alice");

    // 添加数据点到数据序列
    (*set0) << 1 << 2 << 3 << 4 << 5;
    (*set1) << 5 << 0 << 0 << 4 << 3;
    (*set2) << 3 << 5 << 8 << 13 << 8;

    // 创建一个数据序列，把每个人的数据统一起来
    QBarSeries *series1 = new QBarSeries();
    series1->append(set0);
    series1->append(set1);
    series1->append(set2);

    // 将数据序列添加到柱状图中
    chart->addSeries(series1);

    // 创建一个坐标轴
    QBarCategoryAxis *axis = new QBarCategoryAxis();
    axis->append(QStringList() << "Jan" << "Feb" << "Mar" << "Apr" << "May");

    // 将坐标轴设置到柱状图中
    chart->addAxis(axis, Qt::AlignLeft);
    series1->attachAxis(axis);//把坐标轴和数据合并到一起
    this->ui->widget_2->setChart(chart);
    chart->setAnimationOptions(QChart::AllAnimations);



    // 创建一个饼状图
    QPieSeries *series2 = new QPieSeries();
    series2->append("Slice 1", 10);
    series2->append("Slice 2", 20);
    series2->append("Slice 3", 30);
    series2->append("Slice 4", 40);

    // 创建一个饼图并添加系列
    chart = new QChart();
    chart->addSeries(series2);
    chart->setTitle("Simple Pie Chart");
    chart->setAnimationOptions(QChart::AllAnimations);

    // 创建一个显示饼图的小部件
//    chartView = new QChartView(chart);
//    chartView->setRenderHint(QPainter::Antialiasing);

    // 创建主窗口并设置布局
    ui->widget_3->setChart(chart);
}

MainWindow::~MainWindow()
{
    delete ui;
}

